package com.qjl.shopping.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qjl.shopping.bean.User;
import com.qjl.shopping.common.ResponseEntity;
import com.qjl.shopping.controller.vm.UserVM;
import com.qjl.shopping.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author liguanghui
 * @Date 2023/5/1
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;


    //    注册用户
    @PostMapping("/insert/user")
    public ResponseEntity insertUser(@RequestBody UserVM user){
        userService.register(user);
        return new ResponseEntity(200,"注册成功");
    }

    //    登录
    @GetMapping("/user/login")
    public ResponseEntity login(String name,String password){
        UserVM user = userService.login(name,password);
        return ResponseEntity.success("登录成功", user);
    }
    //修改
    @PostMapping("/update/user")
    public ResponseEntity updateUser(@RequestBody User user){
        userService.updateById(user);
//        System.out.println(user);
        return ResponseEntity.success("修改用户信息成功");
    }

    //查询
    @GetMapping("/find/user/{id}")
    public ResponseEntity findUserById(@PathVariable int id){
        User byId = userService.getById(id);
        if(byId==null){
            return ResponseEntity.error("无该租客");
        }
        return ResponseEntity.success("根据id查询用户成功",byId);
    }

    //查询全部
    @GetMapping("/find/all/users")
    public ResponseEntity findUsers(@RequestParam(defaultValue = "0") int pageNum,
                                    @RequestParam(defaultValue = "9") int pageSize){
        Page<User> page = userService.selectPage(pageNum, pageSize);
        return ResponseEntity.success("查询全部用户成功",page);
    }
}
